package com.qms.repast.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qms.repast.App;
import com.qms.repast.R;
import com.qms.repast.aspectj.login.CheckLogin;
import com.qms.repast.commons.Constants;
import com.qms.repast.entity.reqbean.ShopId;
import com.qms.repast.entity.resbean.ShopDetailInfo;
import com.qms.repast.entity.resbean.ShopDetailInfo.BodyBean.ShopRecommendListBean;
import com.qms.repast.net.GlideImageLoader;
import com.qms.repast.ui.adapter.DiningAdapter;
import com.qms.repast.ui.base.BaseActivity;
import com.qms.repast.ui.presenter.DiningPresenter;
import com.qms.repast.ui.view.IDiningView;
import com.qms.repast.utils.AppUtils;
import com.qms.repast.utils.SpUtils;
import com.qms.repast.weidgets.common.MyGrideView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author 泅渡者
 * @date 2018/9/17 0017
 * @describe: 餐厅详情界面
 */
public class DiningActivity extends BaseActivity<DiningPresenter> implements IDiningView {
	@BindView(R.id.iv_back)
	ImageView ivBack;
	@BindView(R.id.tv_title)
	TextView tvTitle;
	@BindView(R.id.tv_rightImg)
	ImageView tvRightImg;
	@BindView(R.id.banner)
	Banner banner;
	@BindView(R.id.tv_diningName)
	TextView tvDiningName;
	@BindView(R.id.tv_diningAddress)
	TextView tvDiningAddress;
	@BindView(R.id.tv_distance)
	TextView tvDistance;
	@BindView(R.id.tv_averagePrice)
	TextView tvAveragePrice;
	@BindView(R.id.tv_businessHours)
	TextView tvBusinessHours;
	@BindView(R.id.iv_tel)
	ImageView ivTel;
	@BindView(R.id.mgv_hotList)
	MyGrideView mgvHotList;
	@BindView(R.id.refreshLayout)
	SmartRefreshLayout refreshLayout;
	@BindView(R.id.iv_collect)
	ImageView ivCollect;
	@BindView(R.id.iv_share)
	ImageView ivShare;
	@BindView(R.id.ll_dining)
	LinearLayout llDining;
	@BindView(R.id.ll_order)
	LinearLayout llOrder;
	/**
	 * 推荐菜数据集合
	 */
	private List<ShopRecommendListBean> dishesInfoList;
	/**
	 * 推荐菜适配器
	 */
	private DiningAdapter diningAdapter;
	/**
	 * 店铺ID
	 */
	private String shopId;
	private String shopName;
	private String userId;
	private Intent mIntent;

	@Override
	protected int initLayout () {
		return R.layout.activity_dining;
	}

	@Override
	protected void initPresenter (Intent intent) {
		mPresenter = new DiningPresenter(this, this);
		shopId = intent.getStringExtra("shopId");
		shopName = intent.getStringExtra("shopName");
		userId = SpUtils.getParam(AppUtils.getContext(), Constants.USERID, 0) + "";
	}

	@Override
	protected void initView () {
		tvTitle.setText(shopName);
		refreshLayout.setEnableLoadMore(false);
		refreshLayout.setEnableRefresh(false);
		userId = SpUtils.getParam(AppUtils.getContext(), Constants.USERID, 0).toString();
	}

	@Override
	protected void initData () {
		ShopId shopBean = new ShopId();
		shopBean.setShopId(shopId);
		mPresenter.getShopInfo(shopBean);
	}

	@OnClick({R.id.iv_back, R.id.tv_rightImg, R.id.tv_diningAddress, R.id.iv_collect, R.id.iv_share, R.id.ll_order})
	public void onViewClicked (View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				finish();
				break;
			case R.id.tv_rightImg:
				break;
			case R.id.tv_diningAddress:
				break;
			case R.id.iv_collect:
				break;
			case R.id.iv_share:

				break;
			case R.id.ll_order:
				break;
			default:
				break;
		}
	}

	@CheckLogin
	@OnClick(R.id.ll_dining)
	public void onViewClicked () {
		mIntent = new Intent();
		mIntent.setClass(this, CuisineActivity.class);
		mIntent.putExtra("shopName", shopName);
		mIntent.putExtra("shopId", shopId);
		startActivity(mIntent);
	}

	@Override
	protected void onResume () {
		super.onResume();
		userId = SpUtils.getParam(AppUtils.getContext(), Constants.USERID, 0).toString();
	}

	/**
	 * 更新BannerUi
	 */
	private void updateBanner (List<String> bannerList, Banner banner) {
		//设置banner样式
		banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
		//设置图片加载器
		banner.setImageLoader(new GlideImageLoader());
		//设置图片集合
		banner.setImages(bannerList);
		//设置banner动画效果
		banner.setBannerAnimation(Transformer.Default);
		//设置自动轮播，默认为true
		banner.isAutoPlay(true);
		//设置轮播时间
		banner.setDelayTime(3200);
		//设置指示器位置（当banner模式中有指示器时）
		banner.setIndicatorGravity(BannerConfig.CENTER);
		//banner设置方法全部调用完毕时最后调用
		banner.start();
		banner.setOnBannerListener(new OnBannerListener() {
			@Override
			public void OnBannerClick (int position) {

			}
		});
	}

	@Override
	public void showUI (final ShopDetailInfo shopDetailInfo) {
		tvDiningAddress.setText(shopDetailInfo.getBody().getAddress());
		tvAveragePrice.setText(shopDetailInfo.getBody().getAvgMoney() + "");
		tvBusinessHours.setText(shopDetailInfo.getBody().getBusinessStart() + "——" + shopDetailInfo.getBody().getBusinessEnd());

		updateBanner(shopDetailInfo.getBody().getBannerList(), banner);

		ivTel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick (View view) {
				Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + shopDetailInfo.getBody().getTel()));
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		});
		diningAdapter = new DiningAdapter(AppUtils.getContext());
		mgvHotList.setAdapter(diningAdapter);
		diningAdapter.addData(shopDetailInfo.getBody().getShopRecommendList());

		mgvHotList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick (AdapterView<?> adapterView, View view, int i, long l) {
				mIntent = new Intent(DiningActivity.this, CuisineActivity.class);
				mIntent.putExtra("itemId", diningAdapter.getCurrentItem(i).getId());
				mIntent.putExtra("shopName", shopName);
				mIntent.putExtra("shopId", shopId);
				startActivity(mIntent);
			}
		});

		clearData();
	}

	/**
	 * 清空菜品信息和菜单信息，保留订单信息
	 */
	private void clearData () {
		App.getmDaoSession()
				.getCuisineBeanDao()
				.deleteAll();
		App.getmDaoSession()
				.getCuisineMenuBeanDao()
				.deleteAll();
	}

}